package com.hsc.www.validator.test4;

import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
public class StaffController {


    @RequestMapping("/addStaff")
    public @ResponseBody
    String addStaff(@RequestBody @Valid Staff staff, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            StringBuffer stringBuffer = new StringBuffer();
            for (ObjectError error : bindingResult.getAllErrors()) {
                stringBuffer.append(error.getDefaultMessage()).append(";");
            }
            return stringBuffer.toString();
        } else {
            return staff.toString();
        }
    }


}